package com.sam;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;

import javax.persistence.EntityManager;

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Events.Get;
import com.google.api.services.calendar.model.Event;
import com.opensymphony.xwork2.ActionSupport;
import com.sam.entities.Admisor;
import com.sam.entities.Afiliado;
import com.sam.entities.Turno;
import com.sam.utils.EMF;
import com.sam.utils.Utils;

public class Concurrio extends ActionSupport {

	private static final long serialVersionUID = 1702155680693449887L;

	private String user;

	private String calendarId;

	private String eventId;

	private String status = "ERROR";

	private boolean concurrio;

	private String numeroAfiliado;
	
	private static final Logger log = Logger.getLogger(Concurrio.class.getName());

	public String execute() {
		try {
			Event event = getEvent(getCalendarId(), getEventId());
			Turno turno = new Turno();
			turno.setTitulo(event.getSummary());
			turno.setDescripcion(event.getDescription());
			turno.setFechaTurno(new Date(event.getStart().getDateTime().getValue() - (3 * 3600000)));
			turno.setMail(getUser());
			turno.setCalendarId(getCalendarId());
			turno.setEventId(getEventId());
			turno.setConcurrio(isConcurrio());
			turno.setCredencial(getNumeroAfiliado());
			if (event.getExtendedProperties() != null) {
				Map<String, String> shared = event.getExtendedProperties()
						.getShared();
				String numeroAfiliado = shared.get("map_nroAfiliado");
				turno.setCredencial(shared.get("map_nroAfiliado"));					
				log.info("numeroAfiliado " + numeroAfiliado);
				if(numeroAfiliado != null || "".equals(numeroAfiliado)){
					Afiliado obtenerAfiliado = Utils.obtenerAfiliado(numeroAfiliado);
					if(obtenerAfiliado != null){
						turno.setPaciente(obtenerAfiliado.getApellido() + ", " + obtenerAfiliado.getNombre());
					}
				}
			}
			log.info("numeroAfiliado " + getUser());
			Admisor obtenerAdmisor = Utils.obtenerAdmisor(getUser());
			if(obtenerAdmisor != null){
				turno.setProfesional(obtenerAdmisor.getNombre());
			}
			save(turno);
			setStatus("SUCCESS");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return SUCCESS;
	}

	private Event getEvent(String idCalendar, String idEvent)
			throws IOException {
		Calendar calendarService = Utils.getCalendarServices();
		Get get = calendarService.events().get(getCalendarId(), getEventId());
		Utils.setGoogleKey(get);
		Utils.setRequestorId(get, getUser());
		return get.execute();
	}

	private void save(Turno turno) {
		EntityManager em = EMF.get().createEntityManager();
		try {
			em.persist(turno);
		} finally {
			em.close();
		}
	}

	public String getCalendarId() {
		return calendarId;
	}

	public void setCalendarId(String calendarId) {
		this.calendarId = calendarId;
	}

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public boolean isConcurrio() {
		return concurrio;
	}

	public void setConcurrio(boolean concurrio) {
		this.concurrio = concurrio;
	}

	public String getNumeroAfiliado() {
		return numeroAfiliado;
	}

	public void setNumeroAfiliado(String numeroAfiliado) {
		this.numeroAfiliado = numeroAfiliado;
	}

}
